import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query
} from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { QueryFileDto } from './dto/query-file.dto';
import { UpdateFileDto } from './dto/update-file.dto';
import { FilesService } from './files.service';

import { MessagePattern, Payload } from '@nestjs/microservices';

@ApiTags('文件管理')
@Controller('files')
export class FilesController {
  constructor(private readonly filesService: FilesService) {}

  @Post()
  @ApiOperation({
    summary: '创建文件',
    description: '直接创建文件（不通过上传）'
  })
  @ApiResponse({
    status: 201,
    description: '文件创建成功'
  })
  @MessagePattern('createFile')
  async create(@Payload() file) {
   return this.filesService.create(file)
  }

  @Get()
  @ApiOperation({
    summary: '获取文件列表',
    description: '分页获取文件列表'
  })
  @ApiResponse({
    status: 200,
    description: '成功获取文件列表'
  })
  @MessagePattern('findAllFiles')
  async findAll(query: QueryFileDto) {
    return await this.filesService.findAll(query)
  }

  @Get('types')
  @ApiOperation({
    summary: '获取类型文件',
    description: '获取类型定义文件列表'
  })
  @MessagePattern('getTypeFiles')
  async getTypeFiles() {
    return await this.filesService.getTypeFiles();
  }

  @Get('components')
  @ApiOperation({
    summary: '获取组件文件',
    description: '获取组件文件列表'
  })
  @MessagePattern('getComponentFiles')
  async getComponentFiles() {
    return await this.filesService.getComponentFiles();
  }

  @Get(':id')
  @ApiOperation({
    summary: '获取文件详情',
    description: '根据ID获取文件详情'
  })
  @MessagePattern('findOneFile')
  async findOne(@Param('id') id: string) {
    const file = await this.filesService.findOne(+id);
    if (!file) {
      return {
        success: false,
        message: '文件不存在'
      };
    }
    return file
  }

  @Get('name/:filename')
  @ApiOperation({
    summary: '根据文件名获取文件',
    description: '根据文件名获取文件详情'
  })
  @MessagePattern('getFileByName')
  async getFileByName(@Param('filename') filename: string) {
    const file = await this.filesService.findByFilename(filename);
    if (!file) {
      return {
        success: false,
        message: '文件不存在'
      };
    }
    return file
  }

  @Get('content/:id')
  @ApiOperation({
    summary: '获取文件内容',
    description: '根据ID获取文件内容'
  })
  @MessagePattern('getFileContent')
  async getFileContent(@Param('id') id: string) {
    const file = await this.filesService.findOne(+id);
    if (!file) {
      return {
        success: false,
        message: '文件不存在'
      };
    }
    return {
      id: file.id,
      filename: file.filename,
      content: file.content
    }
  }

  @Patch(':id')
  @ApiOperation({
    summary: '更新文件',
    description: '更新文件信息或内容'
  })
  @MessagePattern('updateFile')
  async update(@Param('id') id: string, @Body() updateFileDto: UpdateFileDto) {
    await this.filesService.update(+id, updateFileDto);
    return {
      success: true,
      message: '文件更新成功'
    };
  }

  @Delete(':id')
  @ApiOperation({
    summary: '删除文件',
    description: '删除指定文件'
  })
  @MessagePattern('removeFile')
  async remove(@Param('id') id: string) {
    await this.filesService.remove(+id);
    return {
      success: true,
      message: '文件删除成功'
    };
  }
} 